const { HttpException } = require('../http-exception')

const CatchError = async (ctx, next) => {
  try {
    await next()
  } catch (error) {
    console.log('error :>> ', error);
    if (error instanceof HttpException) {

      ctx.body = {
        msg: error.msg,
        error_code: error.errorCode,
        request: `${ctx.method} ${ctx.path}`,
        // ctx: ctx,
      }
      ctx.status = error.code
    } else {
      ctx.body = {
        msg: error.message,
        error_code: 999,
        request: `${ctx.method} ${ctx.path}`,
        // ctx: ctx,
      }
      ctx.status = 500
    }
  }
}
module.exports = CatchError
